package cn.jhc.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.bean.UserBean;
import cn.jhc.dao.ConnectionFactory;
import cn.jhc.dao.MsgDao;

@WebServlet(name = "Get Book detail", urlPatterns = { "/addmessage.do" })
public class MsgServet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String content = (String) req.getParameter("content");
		String title = (String) req.getParameter("title");
		UserBean user = (UserBean) req.getSession().getAttribute("user");
		if((title.equals("标题不能为空")||title=="")||(content.equals("内容不能为空")||content=="")){
			resp.sendRedirect("msg.jsp");//地址重定向
			return;
		}
		int userid = user.getId();
		MsgDao dao = new MsgDao();
		dao.setCon(ConnectionFactory.getConnection());
		dao.addMessage(userid, title, content);
		RequestDispatcher dispatcher = req.getRequestDispatcher("/chenggong.jsp");
		dispatcher.forward(req, resp);
	}

}
